"use client";
import { GroupType, PrizeTypes } from "@/api/home";
import Box from "@/components/Box";
import { useRouter } from "@/i18n/routing";
import { useSystemStore } from "@/stores/useSystemStore";
import { Toast } from "antd-mobile";
import clsx from "clsx";
import { useTranslations } from "next-intl";
import { FC, useState } from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import HomeGames from "./HomeGames";
// import HomePrize from "./HomePrize";

const buttonGroup = [
    {
        icon: "qianbao3",
        category_name: "sixth",
        isHot: false,
        url: "/freeGames",
        locale: true,
        lock: false,
        isAnimation: true,
        image: "/home/free.png",
    },
    {
        icon: "qianbao3",
        category_name: "seventh",
        isHot: false,
        url: "/replayGames",
        locale: true,
        lock: false,
        image: "/home/replay.png",
    },
    {
        icon: "liwulipinjiangpin",
        category_name: "first",
        isHot: true,
        url: "/promo",
        locale: true,
        lock: false,
        image: "/home/promoto.png",
    },
    // {
    //     icon: "pro-youxi1",
    //     category_name: "second",
    //     isHot: false,
    //     url: "/gameList/110",
    //     locale: true,
    //     lock: true,
    // },
    // {
    //     icon: "huodongyule",
    //     category_name: "third",
    //     isHot: false,
    //     url: "/sports",
    //     locale: true,
    //     lock: false,
    // },
    {
        icon: "shipin",
        category_name: "fourth",
        isHot: false,
        url: "/gameList?gameListFlag=10&type=1&bet_type=1&name=live",
        locale: true,
        lock: false,
        image: "/home/live.png",
    },
    {
        icon: "yingyong",
        category_name: "Fifth",
        isHot: false,
        url: "/download",
        locale: true,
        lock: false,
        image: "/home/app.png",
    },
] as const;

type TabItemType = (typeof buttonGroup)[number] & GroupType;
const TabItem = ({
    item,
    active,
    onClick,
}: {
    item: TabItemType;
    active?: boolean;
    onClick?: (item: TabItemType) => void;
}) => {
    const t = useTranslations("ButtonGroup");
    const router = useRouter();

    const handler = (item: TabItemType) => {
        if (!item.locale) {
            onClick && onClick(item);
            return;
        }

        if (item.lock) {
            Toast.show("It is under development.");
        } else {
            router.push(item.url);
        }
    };
    const cls = clsx("pro-iconfont text-[.34rem]", `pro-${item.icon}`);

    // const cls2 = clsx("iconfont text-[.34rem]", `icon-${item.icon}`);

    const rootCls = clsx(
        "relative flex flex-1 cursor-pointer items-center flex-col",
        active ? "text-[#d0f5fb]" : "text-[#d0f5fb]"
    );

    return (
        <div className={rootCls} onClick={() => handler(item)}>
            <div className="flex h-[.55rem] items-end justify-center">
                {!item.image && <span className={cls}></span>}
                {item.image && (
                    <img
                        src={item.image}
                        width={45}
                        height={55}
                        alt={item.url}
                        className={clsx(item.isAnimation ? "animate-slow-bounce" : "")}
                    ></img>
                )}
            </div>

            <span className={"break-word truncate whitespace-normal text-center text-[0.13rem]"}>
                {item.locale ? t(item.category_name) : item.category_name}
            </span>
            {/* {item.isHot && (
                <img
                    className={"absolute -top-[0.12rem] right-0 h-[0.21rem] w-[0.21rem]"}
                    src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAMvUlEQVR4nO2df3BU1RXHP7ubXyQIIYBaQX6rjNSCotRasKDtTKvOCNI/7A9bRPvDMdOqte2M09Zo61itP+I02jq21OqMbUdaQZ1pq3UkUu0gRaAz1UIFEkDUCoGEJOQHu69z9t63+/L2vd232ZfkBt935k2SzX3nnnu+99x77j337YuhYVkWeVGfWAUsBRYA8/MXjqCxHdgGbAAe9zRKUzL9IxZTVBQmpD5xE9AAjI+sXBLagUZtyywCE1KfmAGsi7whdIjXLAda0oJdhMQ9a6tPLNCuFpERPuZr2y7wkpxLSH2iVo930RA1dBivR59adw1eHtIYecawYLrXRD9wDlHzxp4TrulmYxlNyQ1+c0jD6GrLCYFVzka4CVn+YbfOCGCAzbND1o3xKuDYh8AAJuKk2MOpTlweUvVht8oIosyu2nsdEmHEEBFiGCJCDENEiGGICDEMESGGISLEMESEGIaIEMMQEWIYIkIMQ9mo0tYLchQgpS8bcX3FipJkBAoTIg3urwLL7UyWanA8CYm+wo1PyykHq9xDjgXxVDA5tiw5G9AFJOpg8hkgmecZc6H2FOg8ADs2QfIwHH0XUl1QkYektLwKSCXACotFfW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NBv1TeaG8xnZBmbcSw3mJtvLeYSogYa4WOlloClC8WxnqLiYTcoY21bojrsb1FvmJu+xDXFRgmESLD07l6eCrym2VKwjbdAe4Yxjp9YQIhMmlfq4enbSOoR4MJ3jLShDyko6fHR1gPGyPuLU5ChvNx1O060rlpmIenoBhub8k8y5clpCn9PoWhDgVleLpZ98JS1hTDAdtbbtZ6DxVaaUp6EKIwlJHNet3AIFseJqFR6908RDoNsLmbkKEwlnPLYyjWFMOBFh10hO0t7W6bDySkKdkS8oQWxpaHSQjbWxq0zTPIjbKakg16eCkFovDMELc8TEJY3vJbrxHJL+xdpW8oFkO95WESGrX3D6bzPqRtnANvQpqSR2hKriqyFwzXlodJOKLnxhUBI9RmR7jview3Ofh9u069vGQ2XelybXA7LdquQ8N1+jrRPSIIxEYyOoidnJA5VGyUuxPRpL7fJSbfnwL8H00xaYOiAaDOAAAAAElFTkSuQmCC"
                    alt=""
                />
            )} */}
        </div>
    );
};

interface Props {
    tabs: GroupType[];
    prize?: PrizeTypes[];
}
const Tabs: FC<Props> = (props) => {
    const { tabs, prize } = props;
    const { show_again_game, show_free_game } = useSystemStore((state) => {
        return {
            show_again_game: state.show_again_game,
            show_free_game: state.show_free_game,
        };
    });
    const newButtonGroup = buttonGroup.filter((item: any) => {
        if (item.url === "/freeGames" && show_free_game !== 1) {
            return false;
        }
        if (item.url === "/replayGames" && show_again_game !== 1) {
            return false;
        }
        return true;
    });
    const tabData = [...tabs, ...newButtonGroup];

    const [active, setActive] = useState<number>(0);
    const router = useRouter();
    const selectHandler = (item: TabItemType, index: number) => {
        if (item.bet_type === 2 || item.bet_type === 3) {
            router.push(
                `/gameList/?gameListFlag=${item.category[0].jump_id}&type=1&bet_type=${item.bet_type}`
            );
            return;
        }
        setActive(index);
    };

    const groupGames = tabs[active]?.category;

    return (
        <>
            <Box pt={false} className="px-[.3rem]">
                <Swiper slidesPerView={5} centeredSlides={true} centeredSlidesBounds>
                    {tabData?.map((group, index) => (
                        <SwiperSlide key={index}>
                            <TabItem
                                item={group as TabItemType}
                                active={index === active}
                                onClick={(event) => selectHandler(event, index)}
                            ></TabItem>
                        </SwiperSlide>
                    ))}
                </Swiper>
            </Box>
            {/* <HomePrize data={prize}></HomePrize> */}

            <HomeGames groupGames={groupGames} />
        </>
    );
};

export default Tabs;
